package cn.audit.repository.institution;

import cn.audit.bean.dto.UserCorrelation;
import cn.audit.bean.pojo.AuditInstitution;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @Author: 李忆枫
 * @Date: 2019/5/19 12:25
 */
public interface InstitutionRepository extends JpaRepository<AuditInstitution, Long>, JpaSpecificationExecutor<AuditInstitution> {


    @Query(value = "select new cn.audit.bean.dto.UserCorrelation(a,b) " +
            "from AuditInstitution a,AuditInstitution b " +
            "where a.id=b.superior ")
    List<UserCorrelation> findAuditInstitutionAll();


    /**
     * 根據superior查找
     *
     * @param superior
     * @return
     */
    @Query(value = "select a from AuditInstitution a " +
            "where a.superior=?1 ")
    List<AuditInstitution> findBySuperior(Long superior);


    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    AuditInstitution findAuditInstitutionById(Long id);


    /**
     * 批量删除
     *
     * @param ids
     */
    @Modifying(clearAutomatically = true)
    @Query(value = "DELETE FROM AuditInstitution a  WHERE a.id IN ?1")
    void BatchRemove(List<Long> ids);


    /**
     * 修改
     *
     * @param code
     * @param auditname
     * @param id
     */
    @Modifying(clearAutomatically = true)
    @Query(value = "update AuditInstitution a set a.code=?1,a.auditname=?2 where a.id=?3")
    void batchUpdate(String code, String auditname, Long id);

}
